#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import division

class Curve:
    def __init__(self, mass, color = (0, 0, 0), name = None):
        self.mass = mass
        self.color = color
        self.name = name

class Legends:
    def __init__(self, curves):
        self.curves = curves

    def draw(self, drawer, index):
        for i, curve in enumerate(self.curves):
            text = curve.name + ": " + str(curve.mass[index])
            drawer.draw_down_legend(i, text, curve.color)

class Lines:
    def __init__(self):
        self.xSampleWidth = 0
        self.legends = None
        self.curves = []
        self.index = -1

    def set_sample_width(self, xSampleWidth):
        self.xSampleWidth = xSampleWidth

    def legends_visible(self, flag = True):
        if flag:
            if not self.legends:
                self.legends = Legends(self.curves)
        else: self.legends = None

    def set_curves(self, curves):
        self.curves = curves

    def get_curves(self):
        return self.curves

    def draw(self, drawer, index):
        drawer.set_clipping()
        beg, end = index - self.xSampleWidth - 1, index + 1
        if beg < 0: beg = 0
        if end >= len(self.curves[0].mass): end = len(self.curves[0].mass)
        for curve in self.curves:
            mass = curve.mass[beg:end]
            drawer.draw_step_line(mass, curve.color)
        drawer.destroy_clipping()

        if self.legends:
            self.legends.draw(drawer, end - 1)

    def get_legends_offset(self):
        if self.legends:
            return (len(self.curves), 0)
        else:
            return (0, 0)

if __name__ == "__main__":
    execfile("test_lines.py")


